﻿using System;
using System.Data;
using System.Data.SqlClient;
using Entity;

namespace BLL
{
    public class SiteIdentity : System.Security.Principal.IIdentity
    {
        private string userID;
        private int userType; //0 teacher  1 student
        private string userName;
        private string fullName;
        private string email;
        private string password;

        private TeacherService teacherService = new TeacherService();
        private StudentService studentService = new StudentService();

        public SiteIdentity()
        {
            this.userID = "";
            this.userName = "";
            this.userType = 0;
            this.fullName = "";
            this.email = "";
            this.password = "";
        }

        /// <summary>
        /// 通过用户名来查询是否存在该用户
        /// </summary>
        /// <param name="uname"></param>
        /// <returns></returns>
        public bool GetUserByName(string uname)
        {
            BoTeachers teacher = teacherService.GetTeacherByUserName(uname);
            if (teacher != null)
            {
                userID = teacher.TEA_ID;
                userName = teacher.TEA_USERNAME;
                userType = 0;
                fullName = teacher.TEA_NAME;
                email = teacher.TEA_EMAIL;
                password = teacher.TEA_PASSWORD;
                return true;
            }

            BoStudents student = studentService.GetModelByUserName(uname);
            if (student != null)
            {
                userID = student.STU_ID;
                userName = student.STU_USERNAME;
                userType = 1;
                fullName = student.STU_NAME;
                email = "";
                password = student.STU_PASSWORD;
                return true;
            }
            return false;
        }

        // Properties
        public string AuthenticationType
        {
            get { return "Custom Authentication"; }
        }

        public bool IsAuthenticated
        {
            get
            {
                // assumption: all instances of a SiteIdentity have already
                // been authenticated.
                return true;
            }
        }

        public string UserID
        {
            get { return userID; }
        }

        public string UserName
        {
            get { return userName; }
        }

        public int UserType
        {
            get { return userType; }
        }

        public string Name
        {
            get { return fullName; }
        }

        public string Email
        {
            get { return email; }
        }

        public string Password
        {
            get { return password; }
        }

        public string FullName
        {
            get { return fullName; }
        }
    }
}

